Skip to content

Method: MemoryKeyImpl(Player, Player, Strategy)

1: package de.fhdw.gaming.memory.impl;
2:
3: import java.util.Objects;
4:
5: import de.fhdw.gaming.core.domain.Player;
6: import de.fhdw.gaming.core.domain.Strategy;
7: import de.fhdw.gaming.memory.Key;
8:
9: /**
10: * A Key object that identifies something by a combination of two players and a strategy.
11: */
12: public class MemoryKeyImpl implements Key {
13: /**
14: * Represents the player that "owns" the key.
15: */
16: private final Player<?> player;
17: /**
18: * Represents the Player-Object that the player plays against.
19: */
20: private final Player<?> opponent;
21: /**
22: * Represents the strategy that the player uses.
23: */
24: private final Strategy<?, ?, ?> strategy;
25:
26: /**
27: * Creates a MemoryKeyImpl object with the given parameters.
28: * @param player
29: * @param opponent
30: * @param strategy
31: */
32: public MemoryKeyImpl(Player<?> player, Player<?> opponent, Strategy<?, ?, ?> strategy) {
33: this.player = player;
34: this.opponent = opponent;
35: this.strategy = strategy;
36: }
37:
38: /**
39: * Returns the player "remembering" the outcomes. The strategy saved here belongs to this player.
40: */
41: @Override
42: public Player<?> getPlayer() {
43: return player;
44: }
45:
46: /**
47: * Returns the opponent of the active player.
48: */
49: @Override
50: public Player<?> getOpponent() {
51: return opponent;
52: }
53:
54: /**
55: * Returns the strategy of the active player, used against the opponent.
56: */
57: @Override
58: public Strategy<?, ?, ?> getStrategy() {
59: return strategy;
60: }
61:
62: /**
63: * Compares if two keys are equal depending on the entire strategy and the name of the players.
64: * Otherwise a different outcome would lead to the player not being recognised.
65: */
66: @Override
67: public boolean equals(Object obj) {
68: if (obj == null || this.getClass() != obj.getClass()) {
69: return false;
70: }
71: final MemoryKeyImpl other = (MemoryKeyImpl) obj;
72:
73: return Objects.equals(player.getName(), other.player.getName())
74: && Objects.equals(opponent.getName(), other.opponent.getName())
75: && Objects.equals(strategy, other.strategy);
76: }
77:
78: @Override
79: public int hashCode() {
80: return Objects.hash(player.getName(), opponent.getName(), strategy);
81: }
82: }